var api = require('../../utils/api.js');


Page({

    /**
     * 页面的初始数据
     */
    data: {
        
        lists:[],
        merch:[],
        money:0,
        merch_name:'',
        merch_id:'',
        img:'https://oa.stp1.cn:84/upload/20250212/5ecff9adab8ac72d335425b006bcd80e.jpg',
        logs:1,
        animationData: {},
        showlog:0,
        carid:0,
        cardesc:'',
        carimgs:[],
        caricon:'',
        setmoney:0,
        carmoney:'',
        cardescb:''
    },
    seelogmoney(ret){
        console.log(ret);
        var id=ret.currentTarget.dataset.id;
        var that=this;

        api.post({
            url: 'cars/getmyhistoryquoted',
            data: {id:id},
            success: data => {
              that.setData({
                moneylog:data.data
              })
            }
        })

        this.setData({
            showlog:1
        })
    },
    closelogs(){
        this.setData({
            showlog:0
        })
    },

    setmoney(ret){

        console.log(ret);
        var i=ret.currentTarget.dataset.i;
        var val=ret.detail.value;

        var arr=this.data.lists;
        var id=arr[i].carsid;

        arr[i].price=val;
        console.log(arr);

        this.setData({
            lists:arr
        })

        api.post({
            url: 'cars/setcarmoney',
            data: {id:id,val:val},
            success: data => {
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })
            }
        })



    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
        this.getmerch();
       
    },

    onShow(){
        this.getallgoods();
        this.getdescicons();
    },

    getdescicons(){

        var that=this;
        api.post({
            url: 'index/getdescicons',
            data: {},
            success: data => {
              that.setData({
                descicons:data.data
              })
            }
        })
    },

    getmerch(){

        var that=this;
        api.post({
            url: 'index/getallmerch',
            data: {},
            success: data => {
              that.setData({
                merch:data.data.merch
              })
            }
        })
    },
    getallgoods(){

        var that=this;
        api.post({
            url: 'cars/getallgoodforcar',
            data: {},
            success: data => {
              that.setData({
                  lists:data.data.lists,
                  money:data.data.money
              })
            }
        })
    },
    getallmerch(ret){
        console.log(ret);
        var id=ret.currentTarget.dataset.id;
        var title=ret.currentTarget.dataset.title;

        this.setData({
            merch_name:title,
            merch_id:id
        })
   
        this.closeModal();
    },

    //删除信息
    deletebycar(ret){
        console.log(ret)
        var id=ret.currentTarget.dataset.id;
        var that=this;
        wx.showModal({
          title: '确认删除',
          complete: (res) => {
            if (res.confirm) {
                api.post({
                    url: 'cars/deletebycar',
                    data: {id:id},
                    success: data => {
                      that.getallgoods();
                    }
                })
            }
          }
        })

    },

    //完成创建
    addquoted(){

        var merch_name=this.data.merch_name;
        var merch_id=this.data.merch_id;
        var that=this;

        if(!merch_id){
            wx.showToast({
              title: '请选择',
              icon:'none'
            })

            return false;
        }
         
        api.post({
            url: 'cars/addquoted',
            data: {merchname:merch_name,merchid:merch_id},
            success: data => {
                
                console.log(data);

                wx.showToast({
                    title: data.msg,
                    icon:'none'
                  })
                
                if(data.code==1){
                    wx.navigateTo({
                        url: '/pages/car/quotaion',
                    })
                }
                  

            }
        })

    },

    addcar(){
        wx.navigateTo({
          url: '/pages/car/index',
        })
    },

    showModal() {
        const animation = wx.createAnimation({
          duration: 300, // 动画持续时间
          timingFunction: 'ease-in-out' // 缓动函数
        });
        animation.height('350px').step(); // 设置高度为100%
        this.setData({ 
            animationData: animation.export() 
        });
      },
    closeModal() {
        const animation = wx.createAnimation({
          duration: 300,
          timingFunction: 'ease-in-out'
        });
        animation.height('0%').step(); // 设置高度为0%
        this.setData({ 
            animationData: animation.export() 
        });
    },

    addnewmerch(){
        this.closeModal();
        var that=this;
        wx.showModal({
            title: '创建品牌',
            editable:true,
            placeholderText: '请输入品牌名称',
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定',res)

                console.log(res.content);
                api.post({
                    url: 'index/addmerch',
                    data: {title:res.content},
                    success: data => {

                        wx.showToast({
                          title: data.msg,
                          icon:'none'
                        })
                      that.getmerch();
                    }
                })
              } 
            }
          })
     
    },
    //修改金额
    editmoney(ret){
        var that=this;
        var id=ret.currentTarget.dataset.id;

        this.setData({
            setmoney:1,
            carid:id
        })
    },

    closemoney(){
        this.setData({
            setmoney:0
        })
    },
    // 获取金额
    getmoneys(ret){
        this.setData({
            carmoney:ret.detail.value
        })
    },
    //获取备注
    getcardescb(ret){
        console.log(ret);
        var desc=ret.detail.value;
        this.setData({
            cardescb:desc
        })
    },
    //提交信息
    addmoneydesc(){
        var that=this;
        var cardescb=this.data.cardescb;
        var carmoney=this.data.carmoney;
        var id=this.data.carid;
        var merch_id=this.data.merch_id;

        if(!merch_id){
            wx.showToast({
              title: '请选择品牌',
              icon:'none'
            })
            return false;
        }
       
        api.post({
            url: 'cars/setcarmoneybyid',
            data: {id:id,money:carmoney,desc:cardescb,merch_id:merch_id},
            success: data => {
                wx.showToast({
                    title: data.msg,
                    icon:'none'
                })

                if(data.code==1){
                    that.setData({
                        setmoney:0
                    })
                    that.getallgoods();
                }


                
            }
        })
         
    },
    
    seedescwin(ret){
        var that=this;
        var id=ret.currentTarget.dataset.id;
     

        api.post({
            url: 'cars/getgoodforcarbyid',
            data: {id:id},
            success: data => {
                console.log(data);

                that.setData({
                    cardesc:data.data.lists.desc,
                    carimgs:data.data.lists.images,
                    descicons:data.data.icon,
                    caricon:data.data.lists.icons
                })
            }
        })

        this.setData({
            descwin:1,
            carid:id
        })



    },
    closedesc(){
        this.setData({
            descwin:0
        })
    },
    getcardesc(ret){
        console.log(ret);
        var desc=ret.detail.value;
        this.setData({
            cardesc:desc
        })
    },
   
    uploadpdf(){

        let that = this;
        var usrl=api.HOST;
        var images=[];
        wx.chooseMedia({
            count: 4,
            mediaType: ['image','video'],
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success(res) { 
                console.log(res.tempFiles);
                let tempFilePaths = res.tempFiles;
                console.log(tempFilePaths[0].tempFilePath);
                tempFilePaths.forEach(function(item){
                    wx.uploadFile({
                        filePath: item.tempFilePath,
                        name: 'file',
                        url: usrl+'/api/common/upload',
                        formData:{},
                        success:function(res){
                            var data= JSON.parse(res.data);
                            console.log(data);
                            images.push(data.data.img)
                            that.setData({
                                carimgs:images
                            })
                        },
                        fail: function (res) {
                          console.log('上传失败');
                        }
                    })
                })
            }
        })
    },
    editeicon(ret){
        console.log(ret);
        var i=ret.currentTarget.dataset.i;
        var descicons=this.data.descicons;

        descicons[i].status= !descicons[i].status;

        var str='';
        descicons.forEach(function(item){
            if(item.status==1){
                str+=item.name +',';
            }
        })

        this.setData({
            descicons:descicons,
            caricon:str
        })


    },

    //修改备注信息
    adddesc(){

        var desc=this.data.cardesc;
        var carimg=this.data.carimgs;
        var carid=this.data.carid;
        var caricon=this.data.caricon;
        var that=this;

        api.post({
            url: 'cars/setcargooddesc',
            data: {desc:desc,carimg:carimg,carid:carid,caricon:caricon},
            success: data => {
             

              wx.showToast({
                title: data.msg,
                icon:'none'
              })

              if(data.code==1){
                that.getallgoods();
                that.getdescicons();
                that.setData({
                    descwin:0,
                    carimgs:[],
                    cardesc:'',
                    caricon:''
                  })
              }
            }
        })




    },

    seedesc(ret){
        console.log(ret);
        wx.showToast({
          title: ret.currentTarget.dataset.tit,
          icon:'none'
        })

    }

})